package ly.gupao.edu.thymeleaf;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

/**
 * @author: liyang
 * @date：2018/12/23 0023
 * describe：
 */
@Controller
@RequestMapping("/share")
public class ShareController {

    /**
     * 访问页面，分享页面、错误页面
     * @param key
     * @return
     */
    @GetMapping("/{key}")
    public ModelAndView share(@PathVariable String key){
        ModelAndView modelAndView = new ModelAndView();
        //根据key查询sql，查询出所需要的内容
        //分享人，分享头像，分享文件名称，分享密码 等等一切相关内容都查出来把
        HashMap<String, String> share = getMap(key);
        if(share != null && StringUtils.isNotEmpty(share.get("password"))){
            modelAndView.setViewName("share/password");
        }else{
            modelAndView.setViewName("share/index");
        }
        modelAndView.addAllObjects(share);
        return modelAndView;
    }


    /**
     * 输入密码操作
     * @param password
     * @param key
     * @return
     */
    @PostMapping("/{key}")
    @ResponseBody
    public String unAuth(@RequestBody String password, @PathVariable String key){
        JSONObject jsonObject = new JSONObject();
        HashMap<String, String> share = getMap(key);
        if(share != null){
            JSONObject pass = JSONObject.parseObject(password);
            String jsonPass = pass.getString("password");
            String sharePass = share.get("password");
            if(jsonPass.equals(sharePass)){
                jsonObject.put("code", 200);
                jsonObject.put("data", share);
            }else{
                //密码错误
                jsonObject.put("code", 0000);
                jsonObject.put("errMsg", "密码错误");
            }
        }else{
            //没有该文件
            jsonObject.put("code", 0000);
            jsonObject.put("errMsg", "没有该文件");
        }
        return jsonObject.toString();
    }

    private HashMap getMap(String key){
        HashMap<String, String> unAuth = new HashMap<>();
        unAuth.put("key", key);
        unAuth.put("fileName", "这是一个没有密码的文件.jpg");
        unAuth.put("createTime", "2018-12-12 12:12:12");
        unAuth.put("timeLimit", null);
        unAuth.put("countLimit", "18");
        unAuth.put("fileSize", "3.52G");
        unAuth.put("fileType", "img");
        unAuth.put("fileUrl", "https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/53885b2f.jpg");
        unAuth.put("userHead", "https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/53885b2f.jpg");
        unAuth.put("userName", "你塞呀，小伙汁");
        unAuth.put("userDescription", "行，行，你牛逼行，行，你牛逼行，行，你牛逼行，行，你牛逼");
        HashMap<String, String> auth = new HashMap<>();
        auth.put("key", key);
        auth.put("password", "xxx");
        auth.put("userHead", "https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/53885b2f.jpg");
        auth.put("userName", "你塞呀，小伙汁");
        auth.put("userDescription", "行，行，你牛逼行，行，你牛逼行，行，你牛逼行，行，你牛逼");
        auth.put("fileName", "这是一个没有密码的文件.jpg");
        auth.put("fileUrl", "https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/53885b2f.jpg");
        auth.put("createTime", "2018-12-12 12:12:12");
        auth.put("timeLimit", null);
        auth.put("countLimit", "18");
        auth.put("fileSize", "3.52G");
        auth.put("fileType", "img");
        auth.put("userHead", "https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/53885b2f.jpg");

        HashMap<String, HashMap> map = new HashMap<>();
        map.put("nopass", unAuth);
        map.put("needpass", auth);
        return map.get(key);
    }

}
